package org.jeecg.modules.rental.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.entity.RentalAccount;
import org.jeecg.modules.rental.entity.RentalAccountPackage;
import org.jeecg.modules.rental.entity.RentalEBike;
import org.jeecg.modules.rental.mapper.RentalAccountMapper;
import org.jeecg.modules.rental.mapper.RentalEBikeMapper;
import org.jeecg.modules.rental.req.BindPackageReq;
import org.jeecg.modules.rental.service.IRentalAccountPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

/**
 * @Description: 账户套餐
 * @Author: sean
 * @Date: 2024-06-17
 * @Version: V1.0
 */
@Api(tags = "账户套餐")
@RestController
@RequestMapping("/rental/account-package")
@Slf4j
public class RentalAccountPackageController extends JeecgController<RentalAccountPackage, IRentalAccountPackageService> {
    @Autowired
    private IRentalAccountPackageService rentalAccountPackageService;
    @Autowired
    private RentalAccountMapper rentalAccountMapper;
    @Autowired
    private RentalEBikeMapper rentalEBikeMapper;


    /**
     * 分页列表查询
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    //@AutoLog(value = "账户套餐-分页列表查询")
    @ApiOperation(value = "账户套餐-分页列表查询", notes = "账户套餐-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent="rental/account/packagelist")
    public Result<IPage<RentalAccountPackage>> queryPageList(RentalAccountPackage entity,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<RentalAccountPackage> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<RentalAccountPackage> page = new Page<RentalAccountPackage>(pageNo, pageSize);
        if(StrUtil.isNotBlank(entity.getIdentityName()) || StrUtil.isNotBlank(entity.getAccountPhone())) {
            List<RentalAccount> payAccountList = rentalAccountMapper.selectList(new QueryWrapper<RentalAccount>()
                    .like(StrUtil.isNotBlank(entity.getIdentityName()), "identity_name", "%" + entity.getIdentityName() + "%")
                    .eq(StrUtil.isNotBlank(entity.getAccountPhone()), "phone", entity.getAccountPhone()));
            if(ObjectUtil.isNotEmpty(payAccountList)){
                List<String> accountIds = payAccountList.stream().map((a)-> a.getId().toString()).collect(Collectors.toList());
                entity.setAccountIdList(accountIds);
            }else{
                return Result.OK(page);
            }
        }
        if(StrUtil.isNotBlank(entity.getCarPlate())){
            List<RentalEBike> carList = rentalEBikeMapper.selectList(new QueryWrapper<RentalEBike>()
                    .like(StrUtil.isNotBlank(entity.getCarPlate()), "plate", "%"+entity.getCarPlate()+"%"));
            if(ObjectUtil.isNotEmpty(carList)){
                List<Long> carIds = carList.stream().map(RentalEBike::getId).collect(Collectors.toList());
                entity.setCarIdList(carIds);
            }else{
                return Result.OK(page);
            }
        }
        IPage<RentalAccountPackage> pageList = rentalAccountPackageService.selectJoinPage(page, queryWrapper);
        return Result.OK(pageList);
    }


//    @ApiOperation(value = "退款", notes = "退款")
//    @PostMapping(value = "/refund")
//    public Result<Boolean> refund(@RequestBody AccountPackageReq req) throws Exception {
////        QueryWrapper<RentalAccountPackage> queryWrapper = QueryGenerator.initQueryWrapper(rentalAccountPackage, req.getParameterMap());
////        Page<RentalAccountPackage> page = new Page<RentalAccountPackage>(pageNo, pageSize);
//
//        return  rentalAccountPackageService.refund(req);
//    }


    /**
     * 添加
     *
     * @param rentalAccountPackage
     * @return
     */
    @AutoLog(value = "账户套餐-添加")
    @ApiOperation(value = "账户套餐-添加", notes = "账户套餐-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody RentalAccountPackage rentalAccountPackage) {
        rentalAccountPackageService.save(rentalAccountPackage);
        return Result.OK("common.add.success");
    }

    /**
     * 编辑
     *
     * @param rentalAccountPackage
     * @return
     */
    @AutoLog(value = "账户套餐-编辑")
    @ApiOperation(value = "账户套餐-编辑", notes = "账户套餐-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody RentalAccountPackage rentalAccountPackage) {
        rentalAccountPackageService.updateById(rentalAccountPackage);
        return Result.OK("common.edit.success");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "账户套餐-通过id删除")
    @ApiOperation(value = "账户套餐-通过id删除", notes = "账户套餐-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        rentalAccountPackageService.removeById(id);
        return Result.OK("common.delete.success");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "账户套餐-批量删除")
    @ApiOperation(value = "账户套餐-批量删除", notes = "账户套餐-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.rentalAccountPackageService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("common.delete.batch.success");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "账户套餐-通过id查询")
    @ApiOperation(value = "账户套餐-通过id查询", notes = "账户套餐-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<RentalAccountPackage> queryById(@RequestParam(name = "id", required = true) String id) {
        RentalAccountPackage rentalAccountPackage = rentalAccountPackageService.getById(id);
        if (rentalAccountPackage == null) {
            return Result.error("common.query.not.found", null);
        }
        return Result.OK(rentalAccountPackage);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param rentalAccountPackage
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalAccountPackage rentalAccountPackage) {
        return super.exportXls(request, rentalAccountPackage, RentalAccountPackage.class, "账户套餐");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalAccountPackage.class);
    }


    /**
     * 获取用户有效套餐
     * @return
     */
    @ApiOperation(value = "账户套餐-获取用户有效套餐", notes = "账户套餐-获取用户有效套餐")
    @GetMapping(value = "/effective-packages")
    public Result<List<RentalAccountPackage>> effectivePackages() {
        List<RentalAccountPackage> list = rentalAccountPackageService.effectivePackages();
        return Result.OK(list);
    }

    @ApiOperation(value = "账户套餐-验证", notes = "账户套餐-验证")
    @GetMapping(value = "/verify-package")
    public Result<String> verifyPackage(@RequestParam(name = "accountPackageId", required = true) String accountPackageId) {
        return rentalAccountPackageService.verifyPackage(accountPackageId);
    }

	/**
	 * 行程记录
	 * @return
	 */
	@ApiOperation(value = "账户套餐-行程记录", notes = "账户套餐-行程记录")
	@GetMapping(value = "/history-packages")
	public Result<Page<RentalAccountPackage>> historyPackages(Page page) {
		return Result.OK(rentalAccountPackageService.historyPackages(page));
	}

    @PostMapping("/bind-ebike")
    public Result bindEBike(@RequestBody BindPackageReq req){
        return rentalAccountPackageService.bindEBike(req);
    }

}
